package com.zx.service.shop.Impl;

import com.zx.config.exception.BaseBusinessException;
import com.zx.config.exception.Cart.CartError;
import com.zx.config.exception.cookie.CookieError;
import com.zx.dao.goods.GoodsImgMapper;
import com.zx.dao.shop.ShoppingCartMapper;
import com.zx.dto.request.goods.GoodsRequestDto;
import com.zx.dto.request.shop.CartRequestDto;
import com.zx.dto.response.goods.GoodsImgDto;
import com.zx.dto.response.goods.GoodsListDtoResult;
import com.zx.dto.response.shop.ShoppingCartDto;
import com.zx.enums.GoodsImgTypeEnums;
import com.zx.model.shop.ShoppingCart;
import com.zx.model.user.User;
import com.zx.service.shop.ShoppingCartService;
import com.zx.util.CookieUtil;
import com.zx.util.MallConstant;
import com.zx.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author sunkai
 * @date 2022/8/3 0003 15:09
 * @mood zhou xun
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private GoodsImgMapper goodsImgMapper;

    @Override
    public int insertCart(ShoppingCart shoppingCart, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie:cookies) {
            String name = cookie.getName();
           if (name.equals(MallConstant.TOKEN)){
               String value = cookie.getValue();
               User user = MallConstant.MAP.get(value);
               shoppingCart.setUserId(user.getId());
               break;
           }
        }
        shoppingCart.setCreateTime(new Date());
        shoppingCart.setUpdateTime(shoppingCart.getCreateTime());
        return shoppingCartMapper.insertCart(shoppingCart);
    }

    @Override
    public int queryCart(String userToken) {
        User user = MallConstant.MAP.get(userToken);
        int i = shoppingCartMapper.queryCart(user.getId());
        if (i>0){
            return i;
        }
        throw new BaseBusinessException(CookieError.COOKIE_SELECT_ERROR.getErrorCode(),CookieError.COOKIE_SELECT_ERROR.getErrorCode());
    }

    private GoodsListDtoResult cartPage( GoodsRequestDto goodsRequestDto){
        GoodsListDtoResult goodsListDtoResult=new GoodsListDtoResult();
        goodsListDtoResult.setPageIndex(goodsRequestDto.getPageIndex());
        goodsListDtoResult.setPageSize(goodsRequestDto.getPageSize());
        goodsListDtoResult.setTotalRows(shoppingCartMapper.queryCart(goodsRequestDto.getUid()));
        return goodsListDtoResult;
    }

    @Override
    public GoodsListDtoResult queryUserCart(HttpServletRequest request, GoodsRequestDto goodsRequestDto) {
        Integer index=goodsRequestDto.getPageIndex();
        goodsRequestDto.setPageIndex(PageUtil.pageNumber(goodsRequestDto.getPageIndex(),goodsRequestDto.getPageSize()));
        Map<Integer,GoodsImgDto> mapImg=new HashMap<>();
       /* List<Integer> list=new ArrayList<>();*/
        User user = CookieUtil.queryCookie(request);
        goodsRequestDto.setUid(user.getId());
        List<ShoppingCartDto> shoppingCartDtos = shoppingCartMapper.queryUserCart(goodsRequestDto);
        GoodsListDtoResult goodsListDtoResult = cartPage(goodsRequestDto);
        if (goodsListDtoResult.getTotalRows()<=0){
            return goodsListDtoResult;
        }
        goodsListDtoResult.setPageIndex(index);
        goodsListDtoResult.setTotalPage(PageUtil.totalPage(goodsListDtoResult.getTotalRows(),goodsListDtoResult.getPageSize()));
        List<Integer> integerList=shoppingCartDtos.stream().map(shoppingCartDto -> shoppingCartDto.getGid()).collect(Collectors.toList());
        for (ShoppingCartDto shoppingCartDto:shoppingCartDtos){
            if (goodsRequestDto.getQuantity()!=null){
                shoppingCartDto.setQuantity(goodsRequestDto.getQuantity());
            }
           /* list.add(shoppingCartDto.getId());
            integerList.add(shoppingCartDto.getGid());*/
        }
        List<ShoppingCartDto> shoppingCartDtos1 = cartImge(integerList, mapImg, shoppingCartDtos);
        goodsListDtoResult.setShoppingCartDtoList(shoppingCartDtos1);
        return goodsListDtoResult;
    }


    private List<ShoppingCartDto> cartImge(List<Integer> integerList,Map<Integer,GoodsImgDto> mapImg, List<ShoppingCartDto> shoppingCartDtos){
        List<GoodsImgDto> goodsImgDtoList = goodsImgMapper.queryList(integerList, GoodsImgTypeEnums.COVER.getType());
        mapImg=goodsImgDtoList.stream().collect(Collectors.toMap(GoodsImgDto::getGoodsId,a->a,(k1,k2)->k1));
       /* for(GoodsImgDto goodsImgDto:goodsImgDtoList){
            mapImg.put(goodsImgDto.getGoodsId(),goodsImgDto);
        }*/
        for (ShoppingCartDto shoppingCartDto:shoppingCartDtos){
            GoodsImgDto goodsImgDto = mapImg.get(shoppingCartDto.getGid());
            if (CollectionUtils.isEmpty(shoppingCartDto.getGoodsImgDtoList())){
                shoppingCartDto.setGoodsImgDtoList(new ArrayList<>());
            }
            shoppingCartDto.setGoodsImg(goodsImgDto.getGoodsImg());
        }
        return shoppingCartDtos;
    }

    @Override
    public Boolean addCart(CartRequestDto cartRequestDto,HttpServletRequest request) {
        Date date = new Date();
        User user = CookieUtil.queryCookie(request);
            cartRequestDto.setUserId(user.getId());
            cartRequestDto.setCreateTime(date);
            cartRequestDto.setUpdateTime(date);
            int i = shoppingCartMapper.updateCartGood(cartRequestDto);
            if (i<=0) {
                int i1 = shoppingCartMapper.addCart(cartRequestDto);
                if (i1<=0){
                    throw new BaseBusinessException(CartError.CART_ERROR.getErrorCode(),CartError.CART_ERROR.getErrorMessage());
                }
            }
        return true;
    }

    @Override
    public Boolean deleteCartGood(List<Integer> cid) {
        return shoppingCartMapper.deleteCartGood(cid)==0?false:true;
    }
}
